<?php

namespace zhtfrontend\models\datas;

use common\components\extenders\MenuManage;
use Yii;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use yii\helpers\ArrayHelper;

/**
 * This is the model class for table "{{%goods_price}}".
 *
 * @property integer $id
 * @property integer $goods_id
 * @property string $price_name
 * @property integer $lesson
 * @property string $lesson_price
 * @property integer $distribution_way
 * @property integer $distribution_proportion
 * @property integer $subsidy_type
 * @property string $subsidy_money
 * @property integer $is_scholarship
 * @property string $scholarship
 */
class GoodsPrice extends \zhtfrontend\components\bases\BaseModel
{
    /**
     * @inheritdoc
     */
    public $is_group;

    public static function tableName()
    {
        return '{{%goods_price}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['goods_id', 'lesson', 'distribution_way', 'distribution_proportion', 'subsidy_type', 'is_scholarship','status','created_at','admin_id','is_group','art_lesson','is_art_scholarship','is_activity','effective_number','is_frequency','deadline','back_status'], 'integer'],
            [['lesson_price', 'subsidy_money', 'scholarship','art_lesson_price','art_scholarship','market_price','price'], 'number'],
            [['price_name','lesson_price','distribution_proportion','is_scholarship','subsidy_money','is_activity','market_price'], 'required'],
            [['price_name','deadline_type'], 'string', 'max' => 50],
            [['activity_name','activity_start_time','activity_end_time'], 'string', 'max' => 100],
//            [['art_lesson','art_lesson_price','is_art_scholarship','art_scholarship'],'required','when'=>function(){
//                 return $this->is_group == 1;
//            }],
            [['deadline_type','deadline'],'required','when'=>function(){
                return $this->is_frequency == 0;
            }],
            [['lesson'],'required','when'=>function(){
                return $this->is_frequency == 1;
            }],
            [['activity_start_time','activity_end_time','effective_number'], 'required','when'=>function(){
               return $this->is_activity == 1;
            }],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('zhtfrontend', '自增id'),
            'goods_id' => Yii::t('zhtfrontend', '商品id'),
            'price_name' => Yii::t('zhtfrontend', '价格名称'),
            'activity_name' => Yii::t('zhtfrontend', '活动名称'),
            'lesson' => Yii::t('zhtfrontend', '课次数'),
            'lesson_price' => Yii::t('zhtfrontend', '课次价格'),
            'market_price' => Yii::t('zhtfrontend', '课程原价'),
            'distribution_way' => Yii::t('zhtfrontend', '分成方式 '),
            'distribution_proportion' => Yii::t('zhtfrontend', '分成金额或比例'),
            'subsidy_type' => Yii::t('zhtfrontend', '课时补贴方式'),
            'subsidy_money' => Yii::t('zhtfrontend', '补贴金额'),
            'is_scholarship' => Yii::t('zhtfrontend', '是否支持奖学金'),
            'scholarship' => Yii::t('zhtfrontend', '可使用奖学金金额'),
            'admin_id' => Yii::t('zhtfrontend', '操作人'),
            'status' => Yii::t('zhtfrontend', '状态'),
            'art_lesson' => Yii::t('zhtfrontend', '艺教学课次'),
            'art_lesson_price' => Yii::t('zhtfrontend', '艺教学课次价格'),
            'is_art_scholarship' => Yii::t('zhtfrontend', '艺教学是否可用奖学金'),
            'art_scholarship' => Yii::t('zhtfrontend', '艺教学使用奖学金'),
            'created_at' => Yii::t('zhtfrontend', '创建时间'),
            'is_group' => Yii::t('zhtfrontend', '是否是组合'),
            'is_activity' => Yii::t('zhtfrontend', '是否是活动价格套餐'),
            'activity_start_time' => Yii::t('zhtfrontend', '活动开始时间'),
            'activity_end_time' => Yii::t('zhtfrontend', '活动结束时间'),
            'effective_number' => Yii::t('zhtfrontend', '有效人数'),
            'is_frequency' => Yii::t('zhtfrontend', '是否限制上课次数'),
            'deadline_type' => Yii::t('zhtfrontend', '有效期类型'),
            'deadline' => Yii::t('zhtfrontend', '有效期'),
            'back_status' => Yii::t('zhtfrontend', '后台状态'),
            'price' => Yii::t('zhtfrontend', '单节课次价格'),
        ];
    }

    public function fields(){
        $fields = parent::fields();
        $fields['distribution_way'] = function (){
            switch ($this->distribution_way){
                case 0:
                    return \Yii::t('zhtfrontend','固定分成');
                case 1:
                    return \Yii::t('zhtfrontend','比例分成');
            }
        };
        $fields['subsidy_type'] = function (){
            switch ($this->subsidy_type){
                case 1:
                    return \Yii::t('zhtfrontend','按学生');
                case 2:
                    return \Yii::t('zhtfrontend','课程');
            }
        };
        $fields['is_scholarship'] = function (){
            switch ($this->is_scholarship){
                case 1:
                    return \Yii::t('zhtfrontend','是');
                case 2:
                    return \Yii::t('zhtfrontend','否');
            }
        };
        $fields['status'] = function (){
            switch ($this->status){
                case 1:
                    return \Yii::t('zhtfrontend','启用');
                case 0:
                    return \Yii::t('zhtfrontend','禁用');
            }
        };
        $fields['back_status'] = function (){
            switch ($this->back_status){
                case 1:
                    return \Yii::t('zhtfrontend','启用');
                case 0:
                    return \Yii::t('zhtfrontend','禁用');
            }
        };

        return $fields;
    }

    public function extraFields(){
        return [
            'operate' => function(){
                return (new MenuManage(MenuManage::RECORD_ITEM_TOOLBAR, $this->attributes))->create();
            }
        ];
    }

    public function behaviors()
    {
        return ArrayHelper::merge(parent::behaviors(), [
            [
                'class' => TimestampBehavior::className(),
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at'],
                ],
            ],
        ]);
    }
}
